# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ------------------------------

'''
迭代器：就是把一个类，包装成一个各门用来服务要迭代类的属性时的特殊方法
'''


'''
方法一
'''
# from collections.abc import Iterable
#
# class ClassMate(object):
#     def __init__(self):
#         self.members = list()
#
#     def add(self, mer_name):
#         self.members.append(mer_name)
#
#     def __iter__(self):
#         return ClassmateIterator(self)
#
#
# class ClassmateIterator(object):

#     '''迭代器的类'''

#     def __init__(self, cmobj):
#         self.obj = cmobj
#         self.iter = 0
#
#     def __iter__(self):
#         pass
#
#     def __next__(self):
#         if self.iter < len(self.obj.members):
#             mb_ne = self.obj.members[self.iter]
#             self.iter += 1
#             return mb_ne
#         else:
#             raise StopIteration
#
# def main():
#     classmate.add('张三')
#     classmate.add('李四')
#     classmate.add('王二')
#     # print(classmate.members)    ['张三', '李四', '王二']
#     # for neme in classmate:
#     #     pass
#     # print(isinstance(classmate, Iterable))   error:未加入__iter__方法
#     for name in classmate:
#         print(name)
#     print(isinstance(classmate, Iterable))   # true
#
# if __name__ == '__main__':
#     classmate = ClassMate()
#     main()


# '''
# 方法二
# '''
# from collections.abc import Iterable
#
# class ClassMate(object):
#     def __init__(self):
#         self.members = list()
#         self.iter = 0
#
#     def add(self, mer_name):
#         self.members.append(mer_name)
#
#     def __iter__(self):
#         return self
#
#     def __next__(self):
#         if self.iter < len(self.members):
#             mb_ne = self.members[self.iter]
#             self.iter += 1
#             return mb_ne
#         else:
#             raise StopIteration
#
# def main():
#     classmate.add('张三')
#     classmate.add('李四')
#     classmate.add('王二')
#     # print(classmate.members)    ['张三', '李四', '王二']
#     # for neme in classmate:
#     #     pass
#     # print(isinstance(classmate, Iterable))   error:未加入__iter__方法
#     for name in classmate:
#         print(name)
#     print(isinstance(classmate, Iterable))   # true
#
# if __name__ == '__main__':
#     classmate = ClassMate()
#     main()

'''
斐波那契数列生成
'''

class FinoNacci(object):
    def __init__(self, num_comtent):
        self.nums = num_comtent
        self.current = 0
        self.a = 0
        self.b = 1

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.nums:
            ret =self.a
            self.a, self.b = self.b, self.a + self.b
            self.current += 1
            return ret
        else:
            raise StopIteration


def main():
    num_rqt = int(input('请输入要得到多少位斐波那契数列：'))
    fibonacci = FinoNacci(num_rqt)
    for num in fibonacci:
        print(num)

if __name__ == '__main__':
    main()





